package com.visionet.school.app.web.web.dynamic;

import com.alibaba.fastjson.JSONObject;
import com.visionet.school.app.web.common.utils.PageInfo;
import com.visionet.school.app.web.common.web.WebContextFactory;
import com.visionet.school.app.web.entity.dynamic.vo.DynamicVo;
import com.visionet.school.app.web.entity.enums.MemberType;
import com.visionet.school.app.web.entity.file.vo.FileVo;
import com.visionet.school.app.web.entity.vo.LoginAccountVo;
import com.visionet.school.app.web.entity.vo.StudentVo;
import com.visionet.school.app.web.service.dynamic.DynamicCommentService;
import com.visionet.school.app.web.service.dynamic.DynamicContentService;
import com.visionet.school.app.web.service.dynamic.DynamicService;
import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;

import javax.servlet.http.HttpServletRequest;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * Created by hxpeng on 2017/3/1.
 */

@Controller
@RequestMapping("/dynamic")
public class DynamicController {

    private static final Logger logger = LoggerFactory.getLogger(DynamicController.class);

    @Autowired
    private DynamicService dynamicService;

    @Autowired
    private DynamicCommentService dynamicCommentService;

    @Autowired
    private DynamicContentService dynamicContentService;

    @RequestMapping(value = "/submitUploadImg", method = RequestMethod.POST)
    public ResponseEntity<?> submitUploadImg(@RequestBody JSONObject jsonObject, HttpServletRequest request){
        String serverIds = jsonObject.getString("serverIds");
        Map<String,Object> map = new HashMap<>();
        if(StringUtils.isNotBlank(serverIds)){
            try {
                List<String> list = JSONObject.parseArray(serverIds,String.class);
                if(list != null && list.size() > 0){
                    LoginAccountVo loginAccountVo = WebContextFactory.getWebContext().getLoginAccountVo();
                    List<FileVo> fileList = dynamicService.downLoadWechatImg(list,loginAccountVo.getMemberId());
                    map.put("stat","success");
                    map.put("fileList",fileList);
                }else{
                    map.put("stat","null");
                }
            }catch (Exception e){
                map.put("stat","err");
                map.put("msg",e.getMessage());
                e.printStackTrace();
            }
        }
        return new ResponseEntity(map, HttpStatus.OK);
    }



    @RequestMapping(value = "/saveDynamic",method = RequestMethod.POST)
    public ResponseEntity<?> saveDynamic(@RequestBody DynamicVo dynamicVo){
        if(dynamicVo == null || StringUtils.isBlank(dynamicVo.getContent()) || StringUtils.isBlank(dynamicVo.getTitle())
                || dynamicVo.getClassIds() == null){
            return new ResponseEntity<Object>(null,HttpStatus.BAD_REQUEST);
        }
        Map<String,Object> result = new HashMap<>();
        try {
            dynamicService.save(dynamicVo);
            result.put("stat","success");
        }catch (Exception e){
            result.put("stat","err");
            result.put("msg","保存用户动态失败");
        }
        return new ResponseEntity<Object>(result, HttpStatus.OK);
    }

    /**
     *
     * @param dynamicVo
     * @return
     */
    @RequestMapping(value = "/dynamicList",method = RequestMethod.POST)
    public ResponseEntity<?> dynamicList(@RequestBody DynamicVo dynamicVo){
        if(dynamicVo == null){
            return new ResponseEntity<Object>(null,HttpStatus.BAD_REQUEST);
        }
        if(dynamicVo.getPageInfo() == null){
            dynamicVo.setPageInfo(new PageInfo());
        }
        LoginAccountVo loginAccountVo = WebContextFactory.getWebContext().getLoginAccountVo();
        logger.info("LoginAccount信息:" + loginAccountVo.toString());
        Map<String,Object> result = new HashMap<>();
        try {
            if(loginAccountVo.getMemberType().equals(MemberType.PARENT.toKey())){
                StringBuffer stringBuffer = new StringBuffer("");
                List<StudentVo> studentVoList = loginAccountVo.getStudentVoList();
                logger.info("LoginAccount中的学生集合大小:" + studentVoList.size());
                if(studentVoList != null){
                    for(StudentVo studentVo : studentVoList){
                        logger.info("宝宝信息：" + studentVo.toString());
                        stringBuffer.append(studentVo.getClassId()).append(";");
                    }
                    if(stringBuffer.length() > 1 && studentVoList.size() > 0){
                        if(studentVoList.size() > 1){
                            dynamicVo.setClassId(stringBuffer.substring(0,stringBuffer.length()-1));
                        }else{
                            dynamicVo.setClassId(stringBuffer.toString());
                        }
                    }
                }
            }else if(loginAccountVo.getMemberType().equals(MemberType.TEACHER.toKey())){
                dynamicVo.setClassId(loginAccountVo.getClassId() + ";");
            }
            logger.info("dynamic详细信息："+dynamicVo.toString());
            Map<String,Object> dynamicVoList = dynamicService.dynamicVoList(dynamicVo);
            result.put("stat","success");
            result.put("pageResult",dynamicVoList);
        }catch (Exception e){
            e.printStackTrace();
            result.put("stat","err");
            result.put("msg","获取失败，请稍后再试哦");
        }
        return new ResponseEntity<Object>(result, HttpStatus.OK);
    }


    /**
     * 点赞
     * @param dynamicVo
     * @return
     */
    @RequestMapping(value = "/likeDynamic",method = RequestMethod.POST)
    public ResponseEntity<?> likeDynamic(@RequestBody DynamicVo dynamicVo){
        if(dynamicVo == null || StringUtils.isBlank(dynamicVo.getId())){
            return new ResponseEntity<Object>(null,HttpStatus.BAD_REQUEST);
        }
        HashMap<String,Object> map = new HashMap<>();
        try {
            dynamicService.likeDynamic(dynamicVo.getId());
            map.put("stat","success");
        }catch (Exception e){
            e.printStackTrace();
            map.put("stat","err");
        }
        return new ResponseEntity(map , HttpStatus.OK);
    }


    /**
     * 获取动态详细内容
     * @param dynamicVo
     * @return
     */
    @RequestMapping(value = "/getDynamicInfo",method = RequestMethod.POST)
    public ResponseEntity<?> getDynamicInfo(@RequestBody DynamicVo dynamicVo){
        if(dynamicVo == null || StringUtils.isBlank(dynamicVo.getId())){
            return new ResponseEntity<Object>(null,HttpStatus.BAD_REQUEST);
        }
        HashMap<String,Object> map = new HashMap<>();
        try {
            map.put("stat","success");
            map.put("dynamicInfo",dynamicService.findDynamicInfo(dynamicVo.getId()));
        }catch (Exception e){
            e.printStackTrace();
            map.put("stat","err");
            map.put("msg","获取内容失败！");
        }
        return new ResponseEntity(map , HttpStatus.OK);
    }

}
